package game.Actors;

import game.Config.Config;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;
/**
 * This class represents a Healthbar that can be drawn on the screen.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class HealthBar{
	
	Image healthImage;
	Image healthImageUnscaled;
	Image outlineImage;
	Vector2f position;
	Color color;
	/**
	 * Creates a healthbar at the given position.
	 * A healthbar always uses the healthBarImage, stored in the config class.
	 * @param position	The starting position of the healtbar
	 */
	public HealthBar(Vector2f position) {
		this.outlineImage = Config.healthBarImage.copy();
		this.healthImage = outlineImage.copy().getSubImage(1, 1, Config.HEALTH_BAR_WIDTH, Config.HEALTH_BAR_HEIGHT);
		this.healthImageUnscaled = healthImage.copy();
		this.position = position.copy().add(Config.HEALTH_BAR_OFFSET.copy());
		setPercentage(1); //Full health
	}

	/**
	 * Sets the position of the healthbar.
	 * @param position The position we want the healthbar to have.
	 */
	public void update(Vector2f position){
		this.position = position.copy().add(Config.HEALTH_BAR_OFFSET.copy());
	}
	/**
	 * Draws the healthbar on the screen.
	 */
	public void draw(){
		this.outlineImage.draw(position.x,position.y);
		this.healthImage.draw(position.x+1,position.y+1, color);
		
	}
	/**
	 * Sets the percentHealth left to the given amount.
	 * @param percentage The health percentage we want this healthbar to have.
	 */
	public void setPercentage(float percentage){
		this.setHealthImageColor(percentage);
		this.healthImage = this.healthImageUnscaled.getScaledCopy((int)(healthImageUnscaled.getWidth()*percentage), healthImage.getHeight());
		
	}

	private void setHealthImageColor(float percentage) {
		HealthColors[] allValues = HealthColors.values();
		for(int i = allValues.length-1; i>=0;i--){
			if(allValues[i].healthCap <= percentage){
				this.color = allValues[i].color;
			}
		}
	}
    /**
     * An enumeration containing different states for the healthbar to be in.
     * The enums must be stored in descending order, meaning the 
     * highest health state must be first.
     * @author Sebastian Isheden and Victor Beer.
     *
     */
	private enum HealthColors{
		FULL(Color.green,0.75f),
		WOUNDED(Color.yellow,0.50f),
		HURT(Color.orange,0.25f),
		CRITICAL(Color.red,0f);
		
		private Color color;
	    private float healthCap;
		private HealthColors(Color color, float healthCap){
			this.color = color;
			this.healthCap = healthCap;
		}
		
	}

}

